<template>
  <div class="hyydxq">
    <!-- 选择日期 开始 -->
    <div class="listSearch">
      <div>
        <img src="@/assets/img/sou.png" alt="" class="sou" />
        <input type="text" v-model="params.keyWords" @keyup.enter="onRefresh" placeholder="请输入关键词进行搜索" />
        <img src="@/assets/img/cha.png" alt="" @click="params.keyWords = null" class="cha" />
      </div>
      <img src="@/assets/img/rili.png" alt="" class="rili" @click="dateShow = true" />
    </div>
    <van-calendar v-model="dateShow" :min-date="minDate" type="range" @confirm="onConfirm" color="#37B5B5" />
    <!-- 选择日期 结束 -->
    <!-- 预约信息 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" style="margin-top: 5px">
      <van-list v-model="loading" :finished="finished"  @load="loadList()">
        <template v-if="recordList.length > 0">
          <van-cell v-for="item in recordList" :key="item.id" class="approvalItem content" @click="goInfo(item)">
            <div class="flex-between font-14 line-height-3">
              <div class="col-333333 font-bold-500 font-15 m-r-10">厂内耳标号：{{ item.earLabelNo }}</div>
              <div class="col-999999 line-text-out text-right">{{ item.treatDate }}</div>
            </div>
            <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">畜禽标识编号：{{item.preventionNo}}</div>
          </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 line-text-out text-right">
                疫病类型 <span>{{ item.diseaseTypeCode ? item.diseaseTypeCode.name : '' }}</span>
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 line-text-out text-right">
                疫病名称 <span>{{ item.diseaseName }}</span>
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 line-text-out text-right">
                使用药品 <span>{{ item.drugUseName }}</span>
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 line-text-out text-right">
                创建人 <span>{{ item.createdByName }}</span>
              </div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 line-text-out text-right">
                创建时间 <span>{{ item.createTime }}</span>
              </div>
            </div>
          </van-cell>
        </template>
        <div v-else align="center">
          <img src="@/assets/img/zanwu.png" style="width: 200px; margin-top: 136px" alt="" />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { setValue, getValue, clearValue } from '@/utils/auth'
import { Toast, Cell, CellGroup, Popup, Picker, DatetimePicker, PullRefresh, CheckboxGroup, List, Calendar } from 'vant'
import { diseaseTreatRecordListPage } from '@/api/record'
import dayjs from 'dayjs'

export default {
  components: {
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [CheckboxGroup.name]: CheckboxGroup,
    [Toast.name]: Toast,
    [Calendar.name]: Calendar,

    [List.name]: List
  },
  data() {
    return {
      refreshing: false,
      currentDate: new Date(),
      recordList: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        perPage: 5,
        startDate: null, // 开始日期
        endDate: null, // 结束日期
        keyWords: null // 搜索值
      },
      showPicker1: false,
      flag: null,
      minDate: new Date(2010, 0, 1),
      dateShow: false // 日期弹出框
    }
  },
  watch: {},
  created() {
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.onRefresh()
      clearValue('earTag2d')
      clearValue('fzmSex')
    })
  },
  methods: {
    // 选择日期
    onConfirm(date) {
      const [start, end] = date
      this.dateShow = false
      this.params.startDate = dayjs(start).format('YYYY-MM-DD')
      this.params.endDate = dayjs(end).format('YYYY-MM-DD')
      this.onRefresh()
    },
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false
      this.recordList = []
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.loadList()
    },
    loadList() {
      const houseInfoId = getValue('fzmHourseId')
      this.params.houseInfoId = houseInfoId
      diseaseTreatRecordListPage(this.params).then(res => {
        if (res.status === 0) {
          const datas = res.data.rows
          datas.forEach(ele => {
            const index = ele.treatDate.indexOf(' ')
            ele.treatDate = ele.treatDate.substring(index, 0)
          })
          this.recordList = this.recordList.concat(res.data.rows)
          this.loading = false
          this.refreshing = false
          this.finished = res.data.rows.length < this.params.perPage
          if (!this.finished) {
            this.params.page += 1
          }
        }
      })
    },
    goInfo(item) {
      setValue('diseaseRecordId', item.id)

      this.$bridge.call('viewDiseaseRecord')
    },
    timeSelection(e) {
      this.showPicker1 = true
      this.flag = e
    },
    onConfirmStartDate() {
      const timestamp = dayjs(this.currentDate).valueOf()
      if (this.flag === 1) {
        if (timestamp <= dayjs(this.params.endDate || '2031-12-31').valueOf()) {
          this.params.startDate = dayjs(this.currentDate).format('YYYY-MM-DD')
        } else {
          Toast('不得晚于结束时间')
          return
        }
      } else {
        if (timestamp >= dayjs(this.params.startDate || '1987-1-1').valueOf()) {
          this.params.endDate = dayjs(this.currentDate).format('YYYY-MM-DD')
          console.log(this.params.endDate)
        } else {
          Toast('不得早于开始时间')
          return
        }
      }
      if (!!this.params.startDate && !!this.params.endDate) {
        this.params.page = 1
        this.refreshing = true
        this.loadList()
      }
      this.showPicker1 = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .van-button--round{
  height:50px
}
// 选择日期开始
.listSearch {
  display: flex;
  align-items: center;
  padding: 0 15px;
  box-sizing: border-box;
  justify-content: space-between;
  padding-top: 10px;
  div {
    width: 310px;
    height: 30px;
    position: relative;
    border-radius: 20px;
    background-color: #fff;
    input {
      height: 100%;
      box-sizing: border-box;
      position: absolute;
      left: 28px;
      width: calc(100% - 50px);
    }
    img {
      width: 12px;
      height: 12px;
      position: absolute;
      top: 50%;
      transform: translate(0, -50%);
    }
    .sou {
      left: 10px;
    }
    .cha {

      right: 10px;
    }
  }
  .rili {
    width: 20px;
    height: 20px;
  }
}
// 选择日期结束
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
</style>
